using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.ACEEngineering.Utility;
using Microsoft.AceEngineering.SPM.SPMBusinessLayer;
using Microsoft.AceEngineering.SPM.SPMCustomDataEntity;

public partial class Admin_ManageAttributes : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
            ValidateUser();
        else
        {
            UserCDE userData = (UserCDE)Session["user"];
            if (userData.Time.AddMinutes(Int32.Parse(ConfigurationManager.AppSettings["AbsoluteExpiration"].ToString())) <= DateTime.Now)
                ValidateUser();
        }

        bool isAdmin = UsersBL.GetUserAdmin(Session["loginname"].ToString());
        if (!isAdmin)
            Response.Redirect("~/Home.aspx");
    }

    private string GiveMeAlias(string domainAndAlias)
    {
        domainAndAlias = domainAndAlias.ToLower();
        char[] strSeperator = new char[] { '\\' };
        string[] aryAuthUser = new string[2];
        aryAuthUser = domainAndAlias.Split(strSeperator);
        return aryAuthUser[1];
    }

    private void ValidateUser()
    {
        string currAlias = GiveMeAlias(HttpContext.Current.User.Identity.Name);
        UserCDE user = UsersBL.ValidateUser(currAlias);
        if (user.Active)
        {
            Session["user"] = user;
            Session["loginname"] = user.UserAlias;
        }
        else
        {
            Response.Redirect(Request.ApplicationPath + "/Errors/NoAccess.htm");
        }
    }

    protected void gridPropertyDisabled_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gridPropertyEnabled.DataBind();
    }

    protected void gridPropertyEnabled_RowDeleted(object sender, GridViewDeletedEventArgs e)
    {
        gridPropertyDisabled.DataBind();
    }

    protected void gridPropertyEnabled_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        foreach (System.Web.UI.Control BaseControl in e.Row.Cells[0].Controls)
        {
            if (BaseControl.GetType().BaseType == typeof(LinkButton))
            {
                LinkButton linkBtn = (LinkButton)BaseControl;
                if (linkBtn.CommandName == "Delete")
                    linkBtn.OnClientClick = "return confirm('Are you sure you want to disable this Property?');";
            }
        }
    }
}
